﻿
using Fozzy.Time;
namespace Fozzy.Family
{
    public class Relative_Age                                                       : Fozzy.Being.Life
    {
        #region Properties
            protected override Template         Template                            { get { return new Template(Statics.Position0 + " | " + Statics.Position1)  ; } }
        #endregion

        #region Constructors

            public Relative_Age(Fozzy.Gedcom.Birth birth, Fozzy.Gedcom.Death death) : base(birth.First, death.First)
            { }
            public Relative_Age(string filter)                                      : base(filter)
            { }

        #endregion

            protected override void Prepare()
            {
                base.Prepare()                                                      ;
                if (!Birth.Be_Unknown)
                {
                    if (Death.Be_Unknown)
                    {
                        Set_Birth()                                                 ;
                        Set_Age(true)                                               ;
                    }
                    else
                    {
                        Set_Birth_Death()                                           ;
                        Set_Age(false)                                              ;
                    }
                }
            }

            private void Set_Age(bool death_is_knwown)
            { 
                if (Be_Alive(Now))
                {
                    Set_Alive()                                                     ;
                }
                else
                {
                    if (!Be_Born(Now))
                    {
                        Set_Not_Born_Yet()                                          ;
                    }
                    else
                    { 
                        Set_Already_Dead()                                          ;
                    }
                }
            }

            private void Set_Birth()
            { 
                Insert(0, new Date(Birth.Year))                                         ;
            }

            private void Set_Already_Dead()
            {
                Insert(0, Birth)                                                        ;
                Insert(1, Death)                                                        ;
            }

            private void Set_Birth_Death()
            { 
                //Name.Set(0, Birth.Year   .ToString() + " - " + Death.Year.ToString()) ;
            }

            

            private void Set_Alive()
            { 
                //Name.Set(1, Age()        .ToString())                                 ;
            }


            private void Set_Not_Born_Yet()
            {
                //Name.Set(1, "-")                                                      ;
            }



        // Living, example: 1900 | 107
            //    if (person.IsLiving)
            //    {
            //        if (person.BirthDate == null)
            //            return string.Empty;

            //        if (!person.Age.HasValue)
            //            return string.Empty;
                        
            //        int age = person.Age.Value - (DateTime.Now.Year - (int)this.displayYear);
            //        return string.Format(CultureInfo.CurrentUICulture,
            //            "{0} | {1}", person.BirthDate.Value.Year, Math.Max(0, age));
            //    }
                
            //    // Deceased, example: 1900 - 1950 | 50                    
            //    if (person.BirthDate != null && person.DeathDate != null)
            //    {
            //        if (!person.Age.HasValue)
            //            return string.Empty;
                        
            //        int age = (this.displayYear >= person.DeathDate.Value.Year) ? 
            //            person.Age.Value : person.Age.Value - (person.DeathDate.Value.Year - (int)this.displayYear);
                
            //        return string.Format(CultureInfo.CurrentUICulture, 
            //            "{0} - {1} | {2}", person.BirthDate.Value.Year,
            //            person.DeathDate.Value.Year, Math.Max(0, age));
            //    }

            //    // Deceased, example: ? - 1950 | ?
            //    if (person.BirthDate == null && person.DeathDate != null)
            //    {
            //        return string.Format(CultureInfo.CurrentUICulture, 
            //            "? - {0} | ?", person.DeathDate.Value.Year);
            //    }

            //    // Deceased, example: 1900 - ? | ?
            //    if (person.BirthDate != null && person.DeathDate == null)
            //    {
            //        return string.Format(CultureInfo.CurrentUICulture,
            //            "{0} - ? | ?", person.BirthDate.Value.Year);
            //    }
                
            //    return string.Empty;
            //}


            //public override void Save()
            //{
            //    // store the person's story into temp directory to be packaged into OPC later
            //            if (p.Story != null)
            //            {
            //                //string storyOldPath = Path.Combine(Path.Combine(
            //                //    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
            //                //    App.ApplicationFolderName), p.Story.RelativePath);
            //                if (File.Exists(storyOldPath))
            //                {
            //                    string storyFile = Path.Combine(storyFolder, Path.GetFileName(p.Story.AbsolutePath));

            //                    // Remove spaces since they'll be packaged as %20, breaking relative paths that expect spaces
            //                    storyFile = ReplaceEncodedCharacters(storyFile);
            //                    p.Story.RelativePath = ReplaceEncodedCharacters(p.Story.RelativePath);

            //                    File.Copy(storyOldPath, storyFile, true);
            //                }
            //            }
            //        }
            //}

    }
}
